﻿package mortal.game.view.npc
{
    import Message.Public.*;
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import frEngine.primitives.object2d.*;
    import frEngine.render.layer.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.common.swfPlayer.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.button.*;
    import mortal.game.view.guide.guideUse.*;
    import mortal.game.view.npc.base.*;
    import mortal.game.view.npc.render.*;
    import mortal.game.view.task.data.*;
    import mortal.game.view.task.view.*;
    import mortal.mvc.core.*;

    public class NpcTaskDialogModule extends NpcTaskDialogModuleBase
    {
        private var _txtDialog:GTextFiled;
        private var _bmpBonus:GBitmap;
        private var _reward:TaskRewardBig;
        private var _txtNpcName:GTextFiled;
        private var _btnFunc:GLabelButton;
        private var _talkBg:Scale9GridImg2DEx;
        private var _rewardBg:Scale9GridImg2DEx;
        private var _rewardLabelContainer:GSprite;
        private var _leftSide:GBitmap;
        private var _rightSide:GBitmap;
        private var _bmpRWJL:GBitmap;
        private var _list:GTileList;
        private var _arrow:SWFPlayer;
        private var _taskInfo:TaskInfo;
        public var showMode:String = "0";
        private var _resumeFunc:Function;
        private var _params:Array;
        private var _timer:Timer;
        private var _curTime:int = -1;
        private var _isAutoFinish:Boolean = false;
        private var _labelLoaded:Boolean = false;
        private var _labelLoading:Boolean = false;
        public var lastNpcId:int;
        public static const ShowMode_TaskInfo:String = "0";
        public static const ShowMode_TaskList:String = "1";
        public static var isShowing:Boolean = false;
        public static var showingTaskInfo:TaskInfo;

        public function NpcTaskDialogModule()
        {
            this.layer = LayerManager.npcDialogLayr;
            return;
        }// end function

        public function resume() : void
        {
            if (this._resumeFunc == null || this._params == null)
            {
                return;
            }
            this.show();
            this._resumeFunc.apply(null, this._params);
            return;
        }// end function

        public function showChoose(param1:DataProvider, param2:TaskInfo, param3:NPCInfo, param4:String) : void
        {
            this._resumeFunc = this.showChoose;
            this._params = [param1, param2, param3, param4];
            this.updateList(param1, NpcChooseRender);
            this.updateTaskInfo(null);
            this.updateNpcInfo(param3);
            this.updateTalk(param4);
            this.showMode = ShowMode_TaskInfo;
            this.updateGuide();
            return;
        }// end function

        public function showSelf(param1:TaskInfo, param2:String = "") : void
        {
            this._resumeFunc = this.showSelf;
            this._params = [param1, param2];
            this._taskInfo = param1;
            this.updateNpcInfo(null);
            this.updateTaskInfo(this._taskInfo);
            this.updateTalk(param2);
            this.updateList();
            this.showMode = ShowMode_TaskInfo;
            this.updateGuide();
            return;
        }// end function

        public function showTaskInfo(param1:TaskInfo, param2:NPCInfo, param3:String) : void
        {
            this._resumeFunc = this.showTaskInfo;
            this._params = [param1, param2, param3];
            this.updateNpcInfo(param2);
            this.updateTaskInfo(param1);
            this.updateTalk(param3);
            this.updateList();
            this.showMode = ShowMode_TaskInfo;
            this.updateGuide();
            return;
        }// end function

        public function showTaskList(param1:DataProvider, param2:NPCInfo, param3:String) : void
        {
            this._resumeFunc = this.showTaskList;
            this._params = [param1, param2, param3];
            this.updateNpcInfo(param2);
            this.updateTalk(param3);
            this.updateList(param1, NpcTaskListRender);
            this.updateTaskInfo(null);
            this.showMode = ShowMode_TaskList;
            this.updateGuide();
            return;
        }// end function

        public function showNpcFunction(param1:DataProvider, param2:NPCInfo, param3:String) : void
        {
            this._resumeFunc = this.showNpcFunction;
            this._params = [param1, param2, param3];
            this.updateNpcInfo(param2);
            this.updateTalk(param3);
            this.updateList(param1, NpcFunctionRender);
            this.updateTaskInfo(null);
            this.showMode = ShowMode_TaskList;
            this.updateGuide();
            return;
        }// end function

        public function showNpcDefaultTalk(param1:NPCInfo, param2:String) : void
        {
            this._resumeFunc = this.showNpcDefaultTalk;
            this._params = [param1, param2];
            this.updateNpcInfo(param1);
            this.updateTalk(param2);
            this.updateList();
            this.updateTaskInfo(null);
            this.showMode = ShowMode_TaskList;
            this.updateGuide();
            return;
        }// end function

        private function updateList(param1:DataProvider = null, param2:Class = null) : void
        {
            if (param1 == null)
            {
                DisplayUtil.removeMe(this._list);
                return;
            }
            if (this._list != null && this._list.parent == null)
            {
                this.contentTopOf3DSprite.addChild(this._list);
            }
            this._list.setStyle("cellRenderer", param2);
            this._list.dataProvider = param1;
            this._list.drawNow();
            return;
        }// end function

        private function updateTaskInfo(param1:TaskInfo = null) : void
        {
            this._taskInfo = param1;
            showingTaskInfo = param1;
            if (this._taskInfo == null)
            {
                DisplayUtil.removeMe(this._rewardLabelContainer);
                DisplayUtil.removeMe(this._reward);
                DisplayUtil.removeMe(this._bmpBonus);
                DisplayUtil.removeMe(this._btnFunc);
                if (_npc3d != null && this._rewardBg.parent != null)
                {
                    _npc3d.removeImg(this._rewardBg);
                }
                return;
            }
            this.contentTopOf3DSprite.addChild(this._btnFunc);
            this.updateLabel();
            this.contentTopOf3DSprite.addChild(this._bmpBonus);
            this._reward.updateRewards(this._taskInfo.stask.rewards);
            this.contentTopOf3DSprite.addChild(this._reward);
            this.contentTopOf3DSprite.addChild(this._rewardLabelContainer);
            if (_npc3d != null && this._rewardBg.parent == null)
            {
                _npc3d.addImg(this._rewardBg, Layer3DManager.backGroudImgLayer);
            }
            if (Cache.instance.role.roleInfo.level < GameConst.TaskAutoDoLv && this._taskInfo.stask.group == ETaskGroup._ETaskGroupMain || param1.isComplete() && this._taskInfo.stask.group == ETaskGroup._ETaskGroupEscort || param1.stask.group == ETaskGroup._ETaskGroupLoop)
            {
                if (this._timer == null)
                {
                    this._curTime = 0;
                    this._timer = new Timer(1000);
                    this._timer.addEventListener(TimerEvent.TIMER, this.timeOutHandler);
                    this._timer.start();
                }
            }
            return;
        }// end function

        private function updateLabel() : void
        {
            if (!this._labelLoaded)
            {
                if (!this._labelLoading)
                {
                    this._labelLoading = true;
                    LoaderHelp.addResCallBack(ResFileConst.taskLabels, this.labelLoaded);
                }
                return;
            }
            if (this._taskInfo == null || this._btnFunc == null)
            {
                return;
            }
            if (this._taskInfo.isCanget())
            {
                this._btnFunc.label = ImagesConst.taskLabel_LQRW;
            }
            else if (this._taskInfo.isDoing())
            {
                this._btnFunc.label = ImagesConst.taskLabel_JXRW;
            }
            else if (this._taskInfo.isComplete() || this._taskInfo.isFail())
            {
                this._btnFunc.label = ImagesConst.taskLabel_WCRW;
            }
            return;
        }// end function

        private function labelLoaded() : void
        {
            this._labelLoading = false;
            this._labelLoaded = true;
            this.updateLabel();
            return;
        }// end function

        private function updateGuide() : void
        {
            if (!TaskUtil.isTaskCurNeedGuide(this._taskInfo))
            {
                this.removeGuide();
                return;
            }
            if (this._arrow == null)
            {
                this._arrow = UICompomentPool.getUICompoment(SWFPlayer) as SWFPlayer;
                this._arrow.timeRate = 2;
                this._arrow.load("guideArrow.swf", ModelType.NormalSwf, null);
                this._arrow.play();
            }
            this._arrow.move(this._btnFunc.x - 50, this._btnFunc.y + 10);
            this.contentTopOf3DSprite.addChild(this._arrow);
            EffectManager.glowFilterReg(this._btnFunc);
            return;
        }// end function

        private function removeGuide() : void
        {
            DisplayUtil.removeMe(this._arrow);
            EffectManager.glowFilterUnReg(this._btnFunc);
            return;
        }// end function

        private function updateNpcInfo(param1:NPCInfo) : void
        {
            _npcInfo = param1;
            if (param1 == null)
            {
                this.lastNpcId = -1;
                this._txtNpcName.text = Cache.instance.role.roleEntityInfo.entityInfo.name;
                addSelf();
            }
            else
            {
                this.lastNpcId = param1.tnpc.code;
                this._txtNpcName.text = param1.tnpc.name;
                addNpc3d();
            }
            return;
        }// end function

        private function updateTalk(param1:String) : void
        {
            if (param1 == null)
            {
                this._txtDialog.htmlText = "";
            }
            else
            {
                this._txtDialog.htmlText = GLinkTextDataParser.parseToHtmlFormat(param1);
            }
            if (this._txtDialog.numLines > 2)
            {
                this._txtDialog.y = 88 - (this._txtDialog.numLines - 2) * 22;
            }
            if (this._txtDialog.y < this._talkBg.y + 20)
            {
                this._txtDialog.y = this._talkBg.y + 20;
            }
            var _loc_2:* = this._txtDialog.textHeight;
            if (_loc_2 > 90)
            {
                this._talkBg.SetSize(430, _loc_2 + 30, 282, 60);
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            var _loc_1:* = GlobalStyle.textFormatPutong;
            _loc_1.leading = 0;
            _loc_1.size = 14;
            _loc_1.letterSpacing = 1;
            _loc_1.color = 14865853;
            this._txtDialog = UIFactory.gTextField("", 290, 88, 390, 120, this.contentTopOf3DSprite, _loc_1);
            this._txtDialog.wordWrap = true;
            this._txtDialog.multiline = true;
            this._txtDialog.filters = [FilterConst.NpcTalkFilter];
            this._bmpBonus = UIFactory.gBitmap("", 315, 151, this.contentTopOf3DSprite);
            this._reward = UICompomentPool.getUICompoment(TaskRewardBig) as TaskRewardBig;
            this._reward.x = 305;
            this._reward.y = 228;
            this.contentTopOf3DSprite.addChild(this._reward);
            _loc_1 = GlobalStyle.textFormatPutong;
            _loc_1.align = TextFormatAlign.CENTER;
            _loc_1.size = 18;
            _loc_1.bold = true;
            _loc_1.letterSpacing = 1;
            _loc_1.color = 14599683;
            this._txtNpcName = UIFactory.gTextField("", 290, 31, 220, 28, this.contentTopOf3DSprite, _loc_1);
            this._txtNpcName.filters = [FilterConst.NpcNameFilter];
            this._txtNpcName.wordWrap = true;
            this._txtNpcName.multiline = true;
            this._btnFunc = UIFactory.gLabelButton(null, GLabelButton.gLoadedButton, ImagesConst.TaskGet_upSkin, 383, 330, 128, 39, this.contentTopOf3DSprite);
            this._list = UIFactory.tileList(315, 228, 358, 86, this.contentTopOf3DSprite);
            this._list.isClickedUpdate = false;
            this._list.direction = GBoxDirection.VERTICAL;
            this._list.rowHeight = 20;
            this._list.columnWidth = 280;
            this._list.setStyle("cellRenderer", NpcTaskListRender);
            _closeBtn.x = 680;
            _closeBtn.y = 8;
            this.resGotHandler();
            this._btnFunc.configEventListener(MouseEvent.CLICK, this.clickFuncBtnHandler);
            Global.stage.addEventListener(MouseEvent.CLICK, this.clickStageHandler);
            this.configEventListener(MouseEvent.CLICK, this.clickMeHandler);
            return;
        }// end function

        private function timeOutHandler(event:TimerEvent) : void
        {
            var _loc_2:String = this;
            var _loc_3:* = this._curTime + 1;
            _loc_2._curTime = _loc_3;
            if (this._isAutoFinish && this._curTime >= 1 && this._taskInfo != null && this._taskInfo.isCanget() && this._taskInfo.stask.group == ETaskGroup._ETaskGroupMain)
            {
                this.clickFuncBtnHandler(null);
            }
            if (this._curTime >= 8)
            {
                this.clickFuncBtnHandler(null);
                this._isAutoFinish = true;
            }
            return;
        }// end function

        public function clickStageHandler2(event:MouseEvent) : Boolean
        {
            if (event.target != Global.stage || LayerManager.isHasFullSceenOpen())
            {
                return false;
            }
            var _loc_2:* = LayerManager.windowLayer.numChildren;
            if (Cache.instance.role.roleInfo.level < 36 && (_loc_2 < 1 || LayerManager.windowLayer.getChildAt(0) is GuideUseWinBase))
            {
                if (this._taskInfo == null)
                {
                    return false;
                }
                if (!this._taskInfo.isCanNotEnd())
                {
                    if (this._reward != null && this._taskInfo.isComplete() && !this._taskInfo.isCanNotEnd())
                    {
                        this._reward.checkAndFlyToBackpack();
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TaskAskNextStep, [this._taskInfo, _npcInfo]));
                    this.hide();
                    event.stopImmediatePropagation();
                    return true;
                }
            }
            return false;
        }// end function

        private function clickStageHandler(event:MouseEvent) : void
        {
            if (Rect3DManager.instance.isRectContains(Global.stage.mouseX, Global.stage.mouseY))
            {
                if (this._taskInfo == null)
                {
                    return;
                }
                if (!this._taskInfo.isCanNotEnd())
                {
                    if (this._reward != null && this._taskInfo.isComplete() && !this._taskInfo.isCanNotEnd())
                    {
                        this._reward.checkAndFlyToBackpack();
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TaskAskNextStep, [this._taskInfo, _npcInfo]));
                    this.hide();
                }
            }
            return;
        }// end function

        private function clickMeHandler(event:MouseEvent) : void
        {
            var _loc_2:* = event.target as DisplayObject;
            if (_loc_2 is GButton || _loc_2 is GLoadingButton || _loc_2 is GLoadedButton)
            {
                return;
            }
            if (this._taskInfo == null)
            {
                return;
            }
            if (!this._taskInfo.isCanNotEnd())
            {
                if (this._reward != null && this._taskInfo.isComplete() && !this._taskInfo.isCanNotEnd())
                {
                    this._reward.checkAndFlyToBackpack();
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskAskNextStep, [this._taskInfo, _npcInfo]));
                this.hide();
            }
            return;
        }// end function

        override protected function updateBtnSize() : void
        {
            return;
        }// end function

        private function clickFuncBtnHandler(event:MouseEvent) : void
        {
            this._isAutoFinish = false;
            if (this._taskInfo == null)
            {
                return;
            }
            if (this._reward != null && this._taskInfo.isComplete() && !this._taskInfo.isCanNotEnd())
            {
                this._reward.checkAndFlyToBackpack();
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskAskNextStep, [this._taskInfo, _npcInfo]));
            this.hide();
            return;
        }// end function

        private function resGotHandler() : void
        {
            if (this._bmpBonus == null)
            {
                return;
            }
            var _loc_1:* = UIFactory.bg(0, 0, 774, 390, null, ImagesConst.TaskDialog_bg);
            _bgBitmapData = _loc_1.bitmapData;
            this._talkBg = new Scale9GridImg2DEx(null, GlobalClass.getBitmapData(ImagesConst.NpcTaskTalkBg), true, ResourceConst.getRectangle(ImagesConst.NpcTaskTalkBg));
            this._talkBg.SetSize(420, 100, 282, 60);
            this._rewardLabelContainer = UICompomentPool.getUICompoment(GSprite);
            this._rewardLabelContainer.x = this._talkBg.x + 146;
            this._rewardLabelContainer.y = this._talkBg.y + 115;
            this.contentTopOf3DSprite.addChild(this._rewardLabelContainer);
            this._leftSide = UIFactory.gBitmap(ImagesConst.GroupCopySide, -72, 11, this._rewardLabelContainer);
            this._rightSide = UIFactory.gBitmap(ImagesConst.GroupCopySide, 153, 11, this._rewardLabelContainer);
            this._rightSide.scaleX = -1;
            this._bmpRWJL = UIFactory.gBitmap(ImagesConst.TaskRewardLabel, 0, 0, this._rewardLabelContainer);
            this._rewardBg = new Scale9GridImg2DEx(null, GlobalClass.getBitmapData(ImagesConst.TaskDialogReward_bg), true, ResourceConst.getRectangle(ImagesConst.TaskDialogReward_bg));
            this._rewardBg.SetSize(460, 151, 252, 190);
            if (_img2d)
            {
                _img2d.dispose(false);
            }
            _img2d = new Img2D(null, _bgBitmapData, new Rectangle(0, 0, _bgBitmapData.width, _bgBitmapData.height), true);
            _img2d.autoDispose = false;
            contentTopOf3DSprite.addChild(_closeBtn);
            _isResGot = true;
            if (this.isDisposed)
            {
                return;
            }
            this.updateNpcInfo(_npcInfo);
            _npc3d.addImg(this._talkBg, Layer3DManager.backGroudImgLayer);
            _npc3d.addImg(this._rewardBg, Layer3DManager.backGroudImgLayer);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            Global.stage.removeEventListener(MouseEvent.CLICK, this.clickStageHandler);
            super.disposeImpl(param1);
            _bgBitmapData.dispose();
            _bgBitmapData = null;
            this._bmpBonus.dispose(param1);
            this._bmpBonus = null;
            this._reward.dispose(param1);
            this._reward = null;
            this._btnFunc.dispose(param1);
            this._btnFunc = null;
            this._txtDialog.dispose(param1);
            this._txtDialog = null;
            this._txtNpcName.dispose(param1);
            this._txtNpcName = null;
            this._taskInfo = null;
            _npcInfo = null;
            if (this._list != null)
            {
                this._list.dispose(param1);
                this._list = null;
            }
            this._bmpRWJL.dispose(param1);
            this._bmpRWJL = null;
            this._leftSide.dispose(param1);
            this._leftSide = null;
            this._rightSide.dispose(param1);
            this._rightSide = null;
            this._rewardLabelContainer.dispose(param1);
            this._rewardLabelContainer = null;
            if (this._timer != null)
            {
                this._timer.stop();
                this._timer.removeEventListener(TimerEvent.TIMER, this.timeOutHandler);
                this._timer = null;
            }
            showingTaskInfo = null;
            return;
        }// end function

        override public function stageResize() : void
        {
            this.x = (Global.stage.stageWidth - _bgBitmapData.width) / 2;
            this.y = (Global.stage.stageHeight - _bgBitmapData.height) / 2;
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            this.stageResize();
            isShowing = true;
            return;
        }// end function

        override public function hide() : void
        {
            super.hide();
            isShowing = false;
            return;
        }// end function

    }
}
